package com.zjj.filmcommentapi.utils.config;


import com.zjj.filmcommentapi.controller.WebSocketController;
import com.zjj.filmcommentapi.service.RobotService;
import com.zjj.filmcommentapi.service.UserService;
import com.zjj.filmcommentapi.utils.AuthenticationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.annotation.Resource;

@Configuration
public class WebSocketConfig  {
    @Bean
    public ServerEndpointExporter serverEndpointExporter(){
        return new ServerEndpointExporter();
    }

    /**
     * 因 SpringBoot WebSocket 对每个客户端连接都会创建一个 WebSocketServer（@ServerEndpoint 注解对应的） 对象，Bean 注入操作会被直接略过，因而手动注入一个全局变量
     *
     * @param userService
     */
    @Autowired
    public void setMessageService(UserService userService, RobotService robotService,AuthenticationUtil authenticationUtil) {
        WebSocketController.userService = userService;
        WebSocketController.robotService = robotService;
        WebSocketController.authenticationUtil =authenticationUtil;
    }

}
